<?php

namespace App\Game;

use Carbon\Carbon;
use App\Models\Inf\Player;
use App\Game\Traits\MgTrait;
use App\Models\Def\MainGamePlat;

class mg extends Game
{
    use MgTrait;


    private $platName = 'mg';


    const API_GET_TOKEN = '/lps/j_spring_security_check';


    public function __construct()
    {
        $this->header = [
            'X-Requested-With' => 'X-Api-Client',
            'X-Api-Call' => 'X-Api-Client',
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    //创建用户

    public function curlCreateMember($accountUserName, $password, $request)
    {
        $tokenResult = $this->getApiToken();
        $token = $tokenResult['token'];
        $crId = $tokenResult['id'];
        $this->header = array_merge($this->header, ['X-Api-Auth' => $token, 'Content-Type' => 'application/json']);
        $url = $this->gameUrl . '/lps/secure/network/' . $crId . '/downline';
        $params = [
            'json' => [
                'crId' => $crId,
                'crType' => 'ma',
                'neId' => $crId,
                'neType' => 'ma',
                'tarType' => 'm',
                'username' => $accountUserName,
                'name' => $accountUserName,
                'password' => $password,
                'confirmPassword' => $password,
                'currency' => 'CNY',
                'language' => 'cn',
                'email' => '',
                'mobile' => '',
                'casino' => [
                    'enable' => true
                ],
                'poker' => [
                    'enable' => false
                ]
            ]
        ];

        $output = $this->request('PUT', $url, [], $this->header, $params);
        $output = json_decode($output, true);

        if (array_key_exists('success', $output)) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败:' . $output['message']];
        }
    }

    //获取余额

    public function curlGetBalance($accountUserName)
    {

        $token = $this->getMemberToken($accountUserName);
        $timestamp = Carbon::now('UTC')->format('Y-m-d H:i:s e');
        $this->header = array_merge($this->header, ['Content-Type' => 'application/xml']);
        $url = $this->gameUrl . '/member-api-web/member-api';
        $body = '<mbrapi-account-call 
                timestamp="' . $timestamp . '" 
                apiusername="apiadmin" 
                apipassword="apipassword" 
                token="' . $token . '" 
                />';

        $params = [
            'body' => $body
        ];

        $output = $this->request('POST', $url, [], $this->header, $params);
        $output = $this->xmlToArray($output);

        if ($output['@attributes']['status'] == 0) {
            return ['success' => true, 'data' => ['balance' => $output['wallets']['account-wallet']['@attributes']['credit-balance']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败:' . $output['message']];
        }
    }

    //转入游戏

    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $token = $this->getMemberToken($accountUserName);
        $timestamp = Carbon::now('UTC')->format('Y-m-d H:i:s e');
        $this->header = array_merge($this->header, ['Content-Type' => 'application/xml']);
        $url = $this->gameUrl . '/member-api-web/member-api';
        $body = '<mbrapi-changecredit-call
                timestamp="' . $timestamp . '"
                apiusername="apiadmin"
                apipassword="apipassword"
                token="' . $token . '"
                product="casino"
                operation="topup"
                amount="' . $price . '"
                tx-id="' . $transferid . '"
                />';

        $params = [
            'body' => $body
        ];

        $output = $this->request('POST', $url, [], $this->header, $params);
        $output = $this->xmlToArray($output);

        if ($output['@attributes']['status'] == 0) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        }
    }

    //转出游戏

    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $token = $this->getMemberToken($accountUserName);
        $timestamp = Carbon::now('UTC')->format('Y-m-d H:i:s e');
        $this->header = array_merge($this->header, ['Content-Type' => 'application/xml']);
        $url = $this->gameUrl . '/member-api-web/member-api';
        $body = '<mbrapi-changecredit-call
                timestamp="' . $timestamp . '"
                apiusername="apiadmin"
                apipassword="apipassword"
                token="' . $token . '"
                product="casino"
                operation="withdraw"
                amount="' . $price . '"
                tx-id="' . $transferid . '"
                />';

        $params = [
            'body' => $body
        ];

        $output = $this->request('POST', $url, [], $this->header, $params);
        $output = $this->xmlToArray($output);

        if ($output['@attributes']['status'] == 0) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '此平台没有查询订单功能'];
    }

    //踢线

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    //进入游戏

    public function curlJoinGame($request)
    {
        $token = $this->getMemberToken($request->accountUserName);
        $timestamp = Carbon::now('UTC')->format('Y-m-d H:i:s e');
        $this->header = array_merge($this->header, ['Content-Type' => 'application/xml']);
        $url = $this->gameUrl . '/member-api-web/member-api';
        $demo = (boolean)$request->demo ? 'true' : 'false';
        $body = '<mbrapi-launchurl-call
                timestamp="' . $timestamp . '"
                apiusername="apiadmin"
                apipassword="apipassword"
                token="' . $token . '"
                language="zh"
                gameId="' . $request->gamecode . '"
                bankingUrl=""
                lobbyUrl=""
                logoutRedirectUrl=""
                demoMode="' . $demo . '"
                />';

        $params = [
            'body' => $body
        ];

        $output = $this->request('POST', $url, [], $this->header, $params);
        $output = $this->xmlToArray($output);

        if ($output['@attributes']['status'] == 0) {
            return ['success' => true, 'data' => ['url' => $output['@attributes']['launchUrl']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    public function curlJoinMobileGame($request)
    {
        $token = $this->getMemberToken($request->accountUserName);
        $timestamp = Carbon::now('UTC')->format('Y-m-d H:i:s e');
        $this->header = array_merge($this->header, ['Content-Type' => 'application/xml']);
        $url = $this->gameUrl . '/member-api-web/member-api';
        $demo = (boolean)$request->demo ? 'true' : 'false';
        $body = '<mbrapi-launchurl-call
                timestamp="' . $timestamp . '"
                apiusername="apiadmin"
                apipassword="apipassword"
                token="' . $token . '"
                language="zh"
                gameId="' . $request->gamecode . '"
                bankingUrl=""
                lobbyUrl=""
                logoutRedirectUrl=""
                demoMode="' . $demo . '"
                />';

        $params = [
            'body' => $body
        ];

        $output = $this->request('POST', $url, [], $this->header, $params);
        $output = $this->xmlToArray($output);

        if ($output['@attributes']['status'] == 0) {
            return ['success' => true, 'data' => ['url' => $output['@attributes']['launchUrl']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $tokenResult = $this->getApiToken();
        $token = $tokenResult['token'];
        $crId = $tokenResult['id'];
        $start_time = $startTime->format('Y:m:d:H:i:s');
        $end_time = $endTime->format('Y:m:d:H:i:s');
        $this->header = array_merge($this->header, ['X-Api-Auth' => $token, 'Content-Type' => 'application/json']);
        $url = $this->gameUrl . '/lps/secure/agenttx/' . $crId;
        $params = [
            'start' => $start_time,
            'end' => $end_time,
            'timezone' => "Asia/Shanghai"
        ];

        $output = $this->request('GET', $url, $params, $this->header);
        $output = json_decode($output, true);

        $data = $this->pareReturnData($output);

        return $data;
    }


}